#include "aircraft.h"

aircraft::aircraft(QWidget *parent)
    : QWidget(parent),shrinkMultiple(2),
      speed(10),stop(true)
{
    planeImg=new QPixmap("plane.png");
    int width=planeImg->width();
    int height=planeImg->height();
    planeImgRect=new QRect(10,10,width,height);

    planeTimer=new QTimer(this);

    connect(this,SIGNAL(clicked()),this,SLOT(mousePressEventSLOT()));
    connect(planeTimer,SIGNAL(timeout()),this, SLOT(run()));
}

void aircraft::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(*planeImgRect,*planeImg);
}

void aircraft::mousePressEvent(QMouseEvent *event)
{
    if(planeImgRect->contains(event->x(),event->y()))
    {
        stop=!stop;
        emit clicked();
    }
}
void aircraft::run()
{
    int width=planeImg->width();
    planeImgRect->setX(planeImgRect->x()+10);
    planeImgRect->setWidth(width);
    repaint();
}

void aircraft::mousePressEventSLOT()
{
    if(!stop)
    {
        planeTimer->start(20);
    }
    else
    {
        planeTimer->stop();
    }
}

aircraft::~aircraft()
{
    delete planeImg;
    delete planeImgRect;
    delete planeTimer;

}
